package com.neusoft.controller;

import com.neusoft.mapper.TokenMapper;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Service;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class AuthenticationFilter implements GlobalFilter {
    //白名单
    private Set<String> whiteListUrlPattern = new HashSet(Arrays.asList(
            "/BusinessController/listAllBusiness",
            "/BusinessController/getBusinessById/**",
            "/CommentController/listCommentByBusinessId/**",
            "/FoodController/listFoodByBusinessId/**",
            "/FoodController/listFoodByFoodId/**",
            "/UserController/getUserByIdByPass/**",
            "/UserController/getUserById/**",
            "/UserController/saveUser/**",
            "/ImageController/listAllImage",
            "/ImageController/listImageById/**",
            "/WalletController/saveWallet/**",
            "/PointController/savePoint/**",
            "/PointController/savePointTrans/**",
            "/UserController/updateUserById/**",
            "/GPTController/query/**"));

    private final AntPathMatcher antMatcher = new AntPathMatcher();

    private TokenMapper userMapper;

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        ServerHttpRequest request = exchange.getRequest();
        String str = request.getHeaders().toString();
        ServerHttpResponse response = exchange.getResponse();
        String url = request.getURI().getPath();
        String token = getToken(request);
//        String userId = request.getQueryParams().get("userId").toString();
        if (shouldFilter(url)) {
//            System.out.println("url 请求未授权");
//            response.setStatusCode(HttpStatus.UNAUTHORIZED);
//            return response.setComplete();
            if (StringUtils.isBlank(token)){
                System.out.println("token 为空");
                response.setStatusCode(HttpStatus.UNAUTHORIZED);
                return response.setComplete();
            } else if (!(token.equals(userMapper.getToken(request.getQueryParams().get("userId").toString())))) {
                System.out.println("token 错误");
                response.setStatusCode(HttpStatus.UNAUTHORIZED);
                return response.setComplete();
            } else {
                System.out.println("url 请求未授权");
                response.setStatusCode(HttpStatus.UNAUTHORIZED);
                return response.setComplete();
            }
        }
        return chain.filter(exchange);
    }

    private boolean shouldFilter(String url) {
        if (CollectionUtils.isNotEmpty(whiteListUrlPattern)) {
            for (String urlPattern : whiteListUrlPattern) {
                if (antMatcher.match(urlPattern, url)) {
                    return false;
                }
            }
        }
        return true;
    }

    private String getToken(ServerHttpRequest request) {
        String token = request.getHeaders().getFirst("token");
        if (StringUtils.isBlank(token)) {
            token = request.getQueryParams().getFirst("token");
        }
        return token;
    }
}
